package com.three.myapplication;

import android.annotation.SuppressLint;
import android.app.PendingIntent;
import android.os.Bundle;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;
import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
    private Button startButton;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        setContentView(R.layout.activity_main);
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
            Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
            return insets;
        });

        startButton = findViewById(R.id.start_button);
        startButton.setOnClickListener(new View.OnClickListener() {
            @SuppressLint("NotificationPermission")
            @Override
            public void onClick(View v) {
                // 启动服务
                startService(new Intent(MainActivity.this, RemoteControlService.class));

                // 创建通知渠道（仅适用于 Android O 及以上版本）
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                    NotificationChannel channel = new NotificationChannel("remote_control_channel",
                            "远程控制通知", NotificationManager.IMPORTANCE_DEFAULT);
                    NotificationManager manager = getSystemService(NotificationManager.class);
                    manager.createNotificationChannel(channel);
                }

                // 创建通知
                Notification.Builder builder = new Notification.Builder(MainActivity.this, "remote_control_channel")
                        .setSmallIcon(android.R.drawable.ic_dialog_info)
                        .setContentTitle("远程控制已启动")
                        .setContentText("点击查看详情");

                // 发送通知并设置点击通知后的意图
                NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
                Notification notification = builder.build();
                Intent intent = new Intent(MainActivity.this, SecondActivity.class);

                // 使用FLAG_UPDATE_CURRENT作为flags参数，以更新现有PendingIntent的额外数据
                PendingIntent pendingIntent = PendingIntent.getActivity(MainActivity.this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
                notification.contentIntent = pendingIntent;




            }
        });
    }
}